package com.sczmgk.hotline_pzh.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.base.BaseActivity;
import com.sczmgk.hotline_pzh.callback.SimplePaserJsonCallback;
import com.sczmgk.hotline_pzh.constants.Constants;
import com.sczmgk.hotline_pzh.dialog.DialogFactory;
import com.sczmgk.hotline_pzh.entity.ComplainInfo;
import com.sczmgk.hotline_pzh.httputils.MyHttp;
import com.sczmgk.hotline_pzh.sp.CacheUtils;
import com.sczmgk.hotline_pzh.utils.CommonUtils;
import com.sczmgk.hotline_pzh.utils.MyToast;
import com.sczmgk.hotline_pzh.utils.Tools;
import com.sczmgk.hotline_pzh.view.ConstomStarView;
import com.sczmgk.hotline_pzh.view.StarLevelEnum;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/2 0002.
 */
public class ComplaintEvaluteActivity extends BaseActivity{

    /*评价内容*/
    private EditText mEtEvalute;

    /*打星级的控件*/
    private ConstomStarView mStar;
    private ComplainInfo complaintInfo;

    /*发表评价的按钮*/
    private Button mBtnEvalute;
    private int starLevel = 5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complaint_evalute);

        //将activity添加到集合中
        CommonUtils.addActivity(this);

        initData() ;

        setHeader() ;

        initView() ;


        setListener() ;
    }

    /**
     * 设置标题栏
     */
    private void setHeader() {
        setTitle(R.string.complaint_evalute_sure);
        setLeftBtnVisibility(View.VISIBLE);
    }

    /**
     * 初始化控件
     */
    private void initView() {

        //输入评价的控件
        mEtEvalute = (EditText) findViewById(R.id.et_complaint_evalute);
        //打星级的控件
        mStar = (ConstomStarView) findViewById(R.id.csv_complaint_star);
        mStar.setStarLevel(starLevel);//默认为5星
        //发表评价的控件
        mBtnEvalute = (Button) findViewById(R.id.btn_evalute);
    }

    /**
     * 初始化数据
     */
    private void initData() {

        //获取传递需要评价的投诉
        Intent intent = getIntent();

        complaintInfo = (ComplainInfo) intent.getSerializableExtra(Constants.COMPLAIN_INFO_KEY);

    }

    /**
     * 设置监听
     */
    private void setListener() {

        mStar.setChangeListener(new ConstomStarView.onStarChangeListener() {
            @Override
            public void onTimeChenged(StarLevelEnum starLevelEnum) {
                switch (starLevelEnum) {
                    case level_1:
                        starLevel = 1;
                        break;
                    case level_2:
                        starLevel = 2;
                        break;
                    case level_3:
                        starLevel = 3;
                        break;
                    case level_4:
                        starLevel = 4;
                        break;
                    case level_5:
                        starLevel = 5;
                        break;

                    default:
                        break;
            }}
        });
        //发表评价的点击事件
        mBtnEvalute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String evaluteInput = mEtEvalute.getText().toString().trim();

                if (Tools.isEmpty(evaluteInput)){
                    MyToast.show(ComplaintEvaluteActivity.this , "请输入评价内容,不少于10个字");
                    return ;
                }

                if (!Tools.isLength(evaluteInput , 20)){
                    MyToast.show(ComplaintEvaluteActivity.this , "请不少于10个字");
                    return ;
                }

                //替换文本中的\n \r为<br/>
                String resultContent = Tools.replaceNewLine(evaluteInput);

                //TODO 需要将星级分数传递到服务器中,
                Map<String,Object> maps = new HashMap<String, Object>();
                maps.put("userid", CacheUtils.getIntData(ComplaintEvaluteActivity.this,Constants.LOGIN_UID,0));
                maps.put("content",resultContent);
                maps.put("id",complaintInfo.id);
                maps.put("score",starLevel);

                MyHttp.requestByCommon(Constants.BASE_URL + Constants.POST_COMMENT_COMPLAIN_URL, maps, MyHttp.POST, ComplaintEvaluteActivity.this, false, null, new SimplePaserJsonCallback() {
                    @Override
                    public void onVerifyIsNull() {
                        showDialog() ;
                    }

                    @Override
                    public void onVerifySuccess(String dataRaw, boolean isCache) {

                    }
                });
            }
        });
    }

    /**
     * 展示评论成功的Dialog
     */
    private void showDialog() {
        DialogFactory.warningDialog(ComplaintEvaluteActivity.this, R.string.evalute_dialog_message, R.string.sure, new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        setResult(RESULT_OK);
                        //关闭所有的窗口
                        CommonUtils.finishActivity();
                    }
                }, 100);
            }
        });
    }


    @Override
    public void handleMessage(Message msg) {

    }

    @Override
    public boolean getIsNeedLogin() {
        return true;
    }


}
